package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.nav;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.NestedScrollView;
import android.view.View;

import com.facebook.drawee.view.SimpleDraweeView;
import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.AccountManager;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.UiInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.commonlyUsed.FeedFragment;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.event.EventFragment;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.orgs.MyOrgsFragment;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseFragment;
import com.graduationdesign.zengyan.graduationdesignapp.utils.Config;
import com.graduationdesign.zengyan.graduationdesignapp.utils.Tools;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NavActiviity extends FragmentActivity {

    @BindView(R.id.navigation)
    BottomNavigationView navigation;

    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;

    @BindView(R.id.nested_scroll_view)
    NestedScrollView nested_content;


    private final BaseFragment[] fragments = new BaseFragment[3];

    private int lastShowFragment = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav_activiity);
        ButterKnife.bind(this);
        initNavBar();
        initFragments();
        initComponent();
        navigation.setBackgroundColor(getResources().getColor(R.color.pink_800));
        Tools.setSystemBarColor(this);


    }

    private void initNavBar() {
        SimpleDraweeView mine = (SimpleDraweeView) findViewById(R.id.mine_avatar);
        mine.setImageURI(Config.imageUrl+AccountManager.getInstance().getAvatarUrl());
        mine.setOnClickListener(v -> {
            Intent intent = UiInjection.provideProfile(this, AccountManager.getInstance().getCurrentStuNum());
            startActivity(intent);
        });

        View search = findViewById(R.id.search_btn);
        search.setOnClickListener(v -> {
            Intent intent = UiInjection.provideSearchPeople(this);
            startActivity(intent);
        });



    }

    private void initFragments() {
        FeedFragment feedFragment = new FeedFragment();
        MyOrgsFragment myOrgsFragment = new MyOrgsFragment();
        EventFragment eventFragment = new EventFragment();

        fragments[0] = feedFragment;
        fragments[1] = myOrgsFragment;
        fragments[2] = eventFragment;

        switchFrament( 0);

    }

    private void initComponent() {

        navigation.setOnNavigationItemSelectedListener(item -> {
            int i = item.getItemId();
            if (i == R.id.navigation_movie) {
                switchFrament(0);
                return true;
            } else if (i == R.id.navigation_music) {
                switchFrament(1);
                return true;
            } else if (i == R.id.navigation_books) {
                switchFrament(2);
                return true;
            }
            return false;
        });

        nested_content.setOnScrollChangeListener((NestedScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {
            if (scrollY < oldScrollY) { // up
                animateNavigation(false);
            }
            if (scrollY > oldScrollY) { // down
                animateNavigation(true);
            }
        });


        Tools.setSystemBarTransparent(this);
    }

    /**
     * 切换Fragment
     *
     * @param index     需要显示的Fragment的索引
     */
    public void switchFrament(int index) {
        if (lastShowFragment != -1 && lastShowFragment == index ) return;
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        if (lastShowFragment != -1 ) transaction.hide(fragments[lastShowFragment]);
        if (!fragments[index].isAdded()) {
            transaction.add(R.id.fragment_container, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();
        lastShowFragment = index;
    }


    boolean isNavigationHide = false;

    public void animateNavigation(final boolean hide) {
        if (isNavigationHide && hide || !isNavigationHide && !hide) return;
        isNavigationHide = hide;
        int moveY = hide ? (2 * navigation.getHeight()) : 0;
        navigation.animate().translationY(moveY).setStartDelay(100).setDuration(300).start();
    }
    boolean isAppBarHide = false;

    private void animateActionBar(final boolean hide) {
        if (isAppBarHide && hide || !isAppBarHide && !hide) return;
        isAppBarHide = hide;
        int moveY = hide ? -(2 * appBarLayout.getHeight()) : 0;
        appBarLayout.animate().translationY(moveY).setStartDelay(100).setDuration(300).start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        fragments[lastShowFragment].onVisible();
    }


}
